package com.zxl.live.screen.call.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.zxl.live.screen.R;
import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.tools.tools.DrawUtils;

/**
 * Created by Administrator on 2017/12/8.
 */
public class InCallActionView extends LinearLayout implements Runnable, View.OnClickListener {

    private boolean isAttached;

    private boolean isAuto;

    private boolean isFullScreen;

    private ImageView mAcceptView;

    private ImageView mRejectView;

    private ObjectAnimator mRotationAnimator;

    private ObjectAnimator mTranslationYAnimator;

    private OnCallActionClickListener mOnCallActionClickListener;

    public InCallActionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CallPreview);
        isFullScreen = a.getBoolean(R.styleable.CallPreview_full_screen, false);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        inflate(getContext(), isFullScreen ?  R.layout.widget_call_action_full_screen : R.layout.widget_call_action, this);
        mAcceptView = ((ImageView) findViewById(R.id.call_accept));
        mAcceptView.setOnClickListener(this);
        mRejectView = ((ImageView) findViewById(R.id.call_reject));
        mRejectView.setOnClickListener(this);
        setGravity(80);

        mTranslationYAnimator = ObjectAnimator.ofFloat(mAcceptView, "translationY", DrawUtils.dip2px(-32)).setDuration(200L);
        mTranslationYAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mTranslationYAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mTranslationYAnimator.setStartDelay(200);

        mRotationAnimator = ObjectAnimator.ofFloat(this.mAcceptView, "rotation", new float[]{-24.0F}).setDuration(200L);
        mRotationAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mRotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mRotationAnimator.addListener(new AnimatorListenerAdapter() {

            public void onAnimationEnd(Animator paramAnonymousAnimator) {
                mAcceptView.setTranslationY(0.0F);
                mAcceptView.setRotation(0.0F);
                if (isAttached && isAuto) {
                    post(InCallActionView.this);
                }
            }
        });
        mRotationAnimator.setStartDelay(200);
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        isAttached = true;
        if (isAuto) {
            post(this);
        }
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isAttached = false;
        stopAnim();
    }


    public void startAnim() {
        post(this);
    }

    public void stopAnim() {
        removeCallbacks(this);
        if (mRotationAnimator.isRunning()) {
            mRotationAnimator.cancel();
            mAcceptView.setRotation(0.0F);
        }

        if (mTranslationYAnimator.isRunning()) {
            mTranslationYAnimator.cancel();
            mAcceptView.setTranslationY(0.0F);
        }
    }

    public void setAutoRun(boolean isAuto) {
        this.isAuto = isAuto;
        if (isAuto) {
            startAnim();
        } else {
            stopAnim();
        }
    }

    @Override
    public void run() {
        mTranslationYAnimator.start();
        mRotationAnimator.start();
    }

    public void refreshTheme(ThemeBean themeBean) {
        if (!TextUtils.isEmpty(themeBean.yes)) {
            Glide.with(getContext()).load(themeBean.yes).placeholder(R.drawable.acb_phone_call_answer).into(mAcceptView);
        } else {
            mAcceptView.setImageResource(R.drawable.acb_phone_call_answer);
        }
        if (!TextUtils.isEmpty(themeBean.no)) {
            Glide.with(getContext()).load(themeBean.no).placeholder(R.drawable.acb_phone_call_refuse).into(mRejectView);
        } else {
            mRejectView.setImageResource(R.drawable.acb_phone_call_refuse);
        }
    }

    public void setInfo(String yes, String no) {
        if (!TextUtils.isEmpty(yes)) {
            Glide.with(getContext()).load(yes).placeholder(R.drawable.acb_phone_call_answer).into(mAcceptView);
        } else {
            mAcceptView.setImageResource(R.drawable.acb_phone_call_answer);
        }
        if (!TextUtils.isEmpty(no)) {
            Glide.with(getContext()).load(no).placeholder(R.drawable.acb_phone_call_refuse).into(mRejectView);
        } else {
            mRejectView.setImageResource(R.drawable.acb_phone_call_refuse);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnCallActionClickListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.call_accept:
                mOnCallActionClickListener.onAcceptClick();
                break;
            case R.id.call_reject:
                mOnCallActionClickListener.onRejectClick();
                break;
        }
    }

    public void setOnCallActionClickListener(OnCallActionClickListener mOnCallActionClickListener) {
        this.mOnCallActionClickListener = mOnCallActionClickListener;
    }

    public interface OnCallActionClickListener {

        void onAcceptClick();

        void onRejectClick();

    }
}
